<template>
  <el-dialog
    :title="isEdit ? '编辑' : '新增'"
    :visible.sync="isShow"
    append-to-body
    width="600px"
    @close="closeDialog"
  >
    <el-form
      ref="form"
      :rules="rules"
      :model="formData"
      label-position="center"
      size="large"
      label-width="120px"
    >
      <el-form-item label="编号" prop="code">
        <el-input
          v-model="formData.code"
          placeholder="请输入编号"
          style="width:200px;"
        />
      </el-form-item>
      <el-form-item label="工艺名称" prop="name">
        <el-input
          v-model="formData.name"
          placeholder="请输入工艺名称"
          style="width:200px;"
        />
      </el-form-item>
      <el-form-item label="热盖温度" prop="tempTop">
        <el-input
          v-model="formData.tempTop"
          type="number"
          placeholder="请输入热盖温度"
          style="width:200px;"
        >
          <template
            slot="append"
          >℃</template>
        </el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取消</el-button>
      <el-button
        type="primary"
        :loading="loading"
        @click="confirmSubmit"
      >确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { qpcrprogProvider } from '@/api/api'
export default {
  name: 'AddNewProcess',
  data() {
    return {
      isShow: false,
      isEdit: false,
      rules: {
        code: [
          { required: true, message: '请输入编号', trigger: 'change' }
        ],
        name: [
          { required: true, message: '请输入工艺名称', trigger: 'change' }
        ],
        tempTop: [
          { required: true, message: '请输入热盖温度', trigger: 'change' }
        ]
      },
      formData: {
        code: '',
        name: '',
        tempTop: ''
      },
      loading: false,
      id: ''
    }
  },
  methods: {
    init(isEdit = false, data) {
      this.isEdit = isEdit
      if (this.isEdit) {
        this.id = data.id
        this.formData.code = data.code
        this.formData.name = data.name
        this.formData.tempTop = data.tempTop
      }
      this.isShow = true
    },
    closeDialog() {
      this.isShow = false
      this.$nextTick(() => {
        this.formData = {
          code: '',
          name: '',
          tempTop: ''
        }
      })
    },
    confirmSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          if (this.isEdit) {
            this.formData.id = this.id
          }
          qpcrprogProvider.save(this.formData).then(res => {
            if (res.code === 0) {
              this.$emit('addSuccess')
              this.closeDialog()
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped></style>
